home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12130 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  65 lines

  1. Newsgroups: comp.lang.c
  2. Path: watserv3.uwaterloo.ca!news
  3. From: bcrwhims@uwaterloo.ca  (Carsten Whimster)
  4. Subject: Re: Settle a bet please
  5. Message-ID: <Dp11Bx.2o7@watserv3.uwaterloo.ca>
  6. Sender: news@watserv3.uwaterloo.ca
  7. Nntp-Posting-Host: cnts2p06.uwaterloo.ca
  8. Reply-To: carsten_whimster@iqpac.com (Carsten Whimster)
  9. Organization: EDM/2
  10. X-Newsreader: IBM NewsReader/2 v1.2.5
  11. References: <4jfopb$o9n@news1.sympatico.ca>
  12. Date: Fri, 29 Mar 1996 11:31:09 GMT
  13.  
  14. In <4jfopb$o9n@news1.sympatico.ca>, Gisele Swinson <gisele.swinson@sympatico.ca> writes:
  15. >In C language, how do they calculate the length of an array.
  16. >
  17. >example
  18. >
  19. >To declare a string "My Name"
  20. >
  21. >is it char name[7] = "My Name"
  22. >or
  23. >is it chat Name[8] = "My Name"
  24.           ^ hmm
  25. >There is a battle in my class whether to include the NULL in the
  26. >array size. 
  27.  
  28. How about 
  29.       char Name[8] = "My Name";
  30. :)
  31. or just
  32.       char Name[] = "My Name";
  33.  
  34. Seriously, you should be able to figure this out by experimentation. Try
  35. it, and use a debugger to examine memory, or even just print out the 
  36. eight chars starting at the 'M'. Make sure that you allocate the next 
  37. few bytes, just in case. It probably wouldn't be necessary, but 
  38. theoretically, those could be the last few bytes that the program has 
  39. access to. Try this: 
  40.  
  41.    int i; char Name[7] = "My Name";
  42.    char More[]  = "AAAAAAA";
  43.  
  44.    for (i = 0; Name[i] != '\0'; i++)
  45.       putchar(Name[i]);
  46.  
  47. If you loop until you see a NULL, you might get "My NameAAAAAAA". There
  48. may be some compiler variation there, but I am not sure about those. 
  49. I am only recently getting relatively familiar with C as a language, 
  50. never mind the platform dependencies. Actually, on my machine (OS/2 and 
  51. IBM VAC++), it prints out:
  52.  
  53. My Name\à
  54.  
  55. So obviously a NULL is needed. Interestingly, the "AAAAAAA" string is 
  56. not placed right after the Name string on my machine. Does anyone know 
  57. what the compiler might be doing here?
  58.  
  59. Carsten Whimster -- carsten_whimster@iqpac.com
  60. -- EDM/2 Associate Editor and Book reviewer
  61. EDM/2   http://www.iqpac.com/edm2/index.shtml
  62. Reviews http://www.iqpac.com/edm2/columns/books.shtml
  63. Webpage http://www.undergrad.math.uwaterloo.ca/~bcrwhims/index.html
  64.  
  65.